package com.liqiye.paymanager.util;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Locale;
import java.util.Set;

/**
 * 货币转换
 */
public class CurrencyConverterUtil {

    private static final Set<String> ZERO_DECIMAL_CURRENCIES = Set.of(
        "BIF", "CLP", "DJF", "GNF", "JPY", "KMF", "KRW", "MGA",
        "PYG", "RWF", "UGX", "VND", "VUV", "XAF", "XOF", "XPF"
    );

    /**
     * 转换成对应货币的最小货币单位值
     */
    public static long amountToMinimun(BigDecimal amount, String currency) {
        if (amount == null || currency == null) {
            throw new IllegalArgumentException("amount/currency must not be null");
        }

        currency = currency.toUpperCase(Locale.ROOT);
        int scale = ZERO_DECIMAL_CURRENCIES.contains(currency) ? 0 : 2;

        BigDecimal multiplier = BigDecimal.TEN.pow(scale);
        return amount.multiply(multiplier).setScale(0, RoundingMode.HALF_UP).longValueExact();
    }

}
